package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Advertisement;
import com.ruoyi.system.domain.vo.AdvertisementRecommendVo;
import com.ruoyi.system.domain.vo.AdvertisementVipDetail;
import com.ruoyi.system.domain.vo.AdvertisementVipVo;
import com.ruoyi.system.service.IAdvertisementService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.utils.GoogleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告Controller
 *
 * @author ruoyi
 * @date 2023-04-26
 */
@RestController
@RequestMapping("/advertisement")
public class AdvertisementController extends BaseController {
    @Autowired
    private IAdvertisementService advertisementService;

    @Autowired
    private ISysUserService iSysUserService;



    /**
     * 查询广告列表
     */
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(Advertisement advertisement) {
        startPage();
        List<Advertisement> list = advertisementService.selectAdvertisementList(advertisement);
        return getDataTable(list);
    }

    /**
     * 导出广告列表
     */
    @Log(title = "广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Advertisement advertisement) {
        List<Advertisement> list = advertisementService.selectAdvertisementList(advertisement);
        ExcelUtil<Advertisement> util = new ExcelUtil<Advertisement>(Advertisement.class);
        util.exportExcel(response, list, "广告数据");
    }

    /**
     * 获取广告详细信息
     */
    @GetMapping(value = "/{advertisementId}")
    @Anonymous
    public AjaxResult getInfo(@PathVariable("advertisementId") Integer advertisementId) {
        List<Map<String, String>> cityData = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        map.put("european","测试数据");
        map.put("street","测试街道");
        map.put("number","5000号");
        map.put("fillin","13134451234");
        map.put("upgrade","0");
        map.put("cityld","100");
        cityData.add(map);
        Advertisement advertisementVipDetail = advertisementService.selectAdvertisementByAdvertisementId(advertisementId);
        advertisementVipDetail.setCityData(cityData);
        return success(advertisementVipDetail);
//        return success(advertisementService.selectAdvertisementByAdvertisementId(advertisementId));
    }

    /**
     * 获取广告详细信息
     */
    @GetMapping(value = "/vipDetail/{advertisementId}")
    @Anonymous
    public AjaxResult selectVipAdvertisementByAdvertisementId(@PathVariable("advertisementId") Integer advertisementId,String isShare) {

        //是否是分享数据
        if(null!=isShare && isShare.equals("1")){
            //todo 此处更新分享次数 暂未来得及写
        }

        List<Map<String, String>> cityData = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        map.put("european","测试数据");
        map.put("street","测试街道");
        map.put("number","5000号");
        map.put("fillin","13134451234");
        map.put("upgrade","0");
        map.put("cityld","100");
        cityData.add(map);
        AdvertisementVipDetail advertisementVipDetail = advertisementService.selectVipAdvertisementByAdvertisementId(advertisementId);
        advertisementVipDetail.setCityData(cityData);
        return success(advertisementVipDetail);
    }

    /**
     * 新增广告
     */
    @Log(title = "广告", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous
    public AjaxResult add(@RequestBody Advertisement advertisement) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        advertisement.setCreateBy(user.getUserId().toString());

        advertisementService.insertAdvertisement(advertisement);
        return success(advertisement.getAdvertisementId());
    }

    /**
     * 修改广告
     */
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Advertisement advertisement) {
        if(null != advertisement.getAdvertisementMoney() && !"".equals(advertisement.getAdvertisementMoney())){
            SysUser user = SecurityUtils.getLoginUser().getUser();
            SysUser userData = iSysUserService.selectUserById(user.getUserId());
            if(Integer.valueOf(userData.getBalance()) < Integer.valueOf(advertisement.getAdvertisementMoney())){
                return AjaxResult.error("余额不足！");
            }
            int data =  Integer.valueOf(userData.getBalance()) - Integer.valueOf(advertisement.getAdvertisementMoney());
            SysUser sysUser = new SysUser();
            sysUser.setUserId(userData.getUserId());
            sysUser.setBalance(String.valueOf(data));
            iSysUserService.updateUser(sysUser);
        }
        return toAjax(advertisementService.updateAdvertisement(advertisement));
    }

    /**
     * 修改广告弹窗数据
     */
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @PutMapping("/updateRemark")
    public AjaxResult editRemark(@RequestBody Advertisement advertisement) {
        return toAjax(advertisementService.updateAdvertisement(advertisement));
    }

    /**
     * 获取广告弹窗信息
     */
    @GetMapping(value = "/getRemark/{advertisementId}")
    @Anonymous
    public AjaxResult getRemark(@PathVariable("advertisementId") Integer advertisementId) {
        return success(advertisementService.selectAdvertisementRemarkByAdvertisementId(advertisementId));
    }

    /**
     * 删除广告
     */
    @Log(title = "广告", businessType = BusinessType.DELETE)
    @PostMapping("/{advertisementIds}")
    public AjaxResult remove(@PathVariable Integer[] advertisementIds) {
        return toAjax(advertisementService.deleteAdvertisementByAdvertisementIds(advertisementIds));
    }

    /**
     * 查询vip广告列表
     */
    @GetMapping("/getVipList")
    @Anonymous
    public TableDataInfo getVipList(Advertisement advertisement) {
        startPage();
        //TODO 配置VIP类型
        advertisement.setCategory("8");
        //TODO 广告状态：发布
        advertisement.setStatus("1");
        List<AdvertisementVipVo> list = advertisementService.selectVipAdvertisementList(advertisement);
        return getDataTable(list);
    }

    /**
     * 查询推荐广告列表
     */
    @GetMapping("/getRecommendList")
    @Anonymous
    public TableDataInfo getRecommendList(Advertisement advertisement) {
        startPage();
        List<AdvertisementRecommendVo> list = advertisementService.getRecommendList(advertisement);
        list.stream().forEach(x->{
            x.setMiles(GoogleUtils.returnGoogleMap());
        });
        return getDataTable(list);
    }

    /**
     * 查询全部广告列表
     */
    @GetMapping("/getAllList")
    @Anonymous
    public TableDataInfo getAllList(Advertisement advertisement) {
        startPage();
        return getDataTable(advertisementService.getAllList(advertisement));
    }

    /**
     * 查询当前州的广告数量
     */
    @GetMapping("/getAdvertisementCount")
    @Anonymous
    public AjaxResult getAdvertisementCount(Advertisement advertisement) {
        startPage();
        return success(advertisementService.getAdvertisementCount(advertisement));
    }

    /**
     * 计算当前广告总金额
     */
    @GetMapping("/getAdvertisementMoney")
    @Anonymous
    public AjaxResult getAdvertisementMoney(Advertisement advertisement) {
        Map<String,String> data = new HashMap<>();
        data.put("advertisementMoney","1500");
        return success(data);
    }

    private void amountCalculation(int money,int userMoney,Long userId){
        int data =  userMoney - money;
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setBalance(String.valueOf(data));
        iSysUserService.updateUser(sysUser);
    }

    /**
     * 查询谷歌地图未知信息
     */
    @GetMapping("/getGoogleMapDetail/{cityDetailName}")
    @Anonymous
    public AjaxResult getGoogleMapDetail(@PathVariable("cityDetailName") String cityDetailName) {
        List<String> dataList = new ArrayList<>();
        dataList.add(cityDetailName);
        dataList.add(cityDetailName + "-模拟数据一");
        dataList.add(cityDetailName + "-模拟数据二");
        dataList.add(cityDetailName + "-模拟数据三");
        return success(dataList);
    }


}
